Java Modifiers
(Tap the post to see more)
Modifiers
Modifiers can be divided into 2 types:
1.
Access Modifiers - controls the access level
2.
Non-Access Modifiers - do not control access level,
but provide other functionality
Access Modifiers:
Java provides many access
modifiers to set access levels for classes, variables, methods, and
constructors.
The four
access levels are −
Package (default) - Visible to the package, the default. No modifiers are needed.
Private - Visible to the class only.
Example 01:
package Modifiers;
public class Private_modifier {
private int privateVariable = 20;
private void privateMethod() {
System.out.println("This is a private method.");
}
public static void main(String[] args) {
Private_modifier myObject = new Private_modifier();
// Accessing the private variable and method within the same class is allowed.
System.out.println("Private variable value: " + myObject.privateVariable);
myObject.privateMethod();
}
}
Public
- Visible to the world
Example 02:
package Modifiers;
public class Public_modifier {
public int publicVariable = 10;
public void publicMethod() {
System.out.println("This is a public method.");
}
public static void main(String[] args) {
Public_modifier myObject = new Public_modifier();
System.out.println("Public variable value: " + myObject.publicVariable);
myObject.publicMethod();
}
}
Protected - Visible to the package and all subclasses
Example 03:
package Modifiers;
//The following code demonstrates the use of the protected access modifier.
//Base class
class Animal {
protected String name;
protected Animal(String name) {
this.name = name;
}
protected void speak() {
System.out.println(name + " makes a sound");
}
}
//Subclass
class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
protected void speak() {
System.out.println(name + " barks");
}
}
public class Protected_modifier {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy");
// Accessing a protected member from a subclass
myDog.speak();
// Accessing a protected member from the same package
Animal myAnimal = new Animal("Generic Animal");
myAnimal.speak();
}
}
Non
– Access Modifiers:
We can
different modifiers for classes, methods, and attributes.
For classes,
modifiers like:
final - The class cannot be inherited by other classes
Example 04:
package Modifiers;
public class Final_modifier {
final int x = 10;
final double PI = 3.14;
public static void main(String[] args) {
Final_modifier myObj = new Final_modifier();
// myObj.x = 50; // will generate an error: cannot assign a value to a final variable
// myObj.PI = 25; // will generate an error: cannot assign a value to a final variable
System.out.println(myObj.x);
}
}
abstract - The class cannot be used to create objects (To access an abstract class, it must be inherited from another class) for methods and attributes, we have as follows:
final - Attributes and methods cannot be
overridden/modified
static - Attributes and methods belong to the class,
rather than an object.
Example 05:
package Modifiers;
public class Static_Modifier {
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
public static void main(String[ ] args) {
myStaticMethod();
Static_Modifier myObj = new Static_Modifier();
myObj.myPublicMethod();
}
}
abstract - Can only be used in an abstract class, and can
only be used on methods. The method does not have a body, for example, abstract
void run(); The body is provided by the subclass (inherited from).
transient - Attributes and methods are skipped when
serializing the object containing them
synchronized - Methods can only be accessed by one thread at a
time
Comments